package com.xujiang.myblog.tools;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;

import java.io.File;
import java.io.IOException;

/**
 * Created by xujiang on 2017/4/14.
 */

public class FileUtils {
    private static final String TAG = FileUtils.class.getSimpleName();



    /**
     * 检查sd卡是否可用
     * */
    public static void isSDCardEnable() throws Exception {
        if (!SDCardUtils.isSDCardEnable()) {
            throw new Exception("SD卡不存在或不可用！");
        }
    }

    /**
     * 空间余量检查
     * */
    public static void isSDLessMB(long mb) throws Exception {
        if (SDCardUtils.getSDAvailableSize() < mb * 1024) {
            throw new Exception("SD卡空间不足" + mb + "MB！");
        }
    }

    /**
     * SD {@link Context#getExternalCacheDir()} 建立缓存文件夹
     * @param cacheDir 缓存目录名
     * @return {@link File} 以建立的缓存路径
     */
    public static File createCacheSDAndroidDirectory(String cacheDir) throws Exception {
        isSDCardEnable();
        isSDLessMB(100);
        File parentFile = Common.getSDExternalPath();
        return createHideMediaDirectory(createPath(parentFile.getAbsolutePath(), cacheDir));
    }

    //http://blog.csdn.net/jaycee110905/article/details/21130557
    //得到应用一同删除的目录
    public static File getSDExternalPath(@NonNull Context context) {
        File externalCacheDir = context.getExternalCacheDir();
        if (externalCacheDir == null) {
            return context.getCacheDir();
        }
        return externalCacheDir.getParentFile();
    }


    /**
     * 创建路径
     * @param rootPath 父路径
     * @param path  建立的路径
     * @return  {@link File}
     * @throws Exception
     */
    public static File createPath(String rootPath, String path) throws Exception {
        return createPath(new File(rootPath, path));
    }


    /**
     * 创建路径
     * @param path 路径
     * @return  {@link File}
     * @throws Exception
     */
    public static File createPath(File path) throws Exception {
        if (!path.exists()) {
            if (!path.mkdirs()) {
                throw new Exception("创建路径失败:" + path.getAbsolutePath());
            } else {
                Log.d(TAG, "create path:" + path.getAbsolutePath());
            }
        }
        return path;
    }


    /**
     * 建立隐藏所有媒体文件的文件夹
     * @param path 缓存目录
     * @return {@link File}
     */
    public static File createHideMediaDirectory(File path) throws Exception {
        try {
            if (!path.exists()) {
                if (path.mkdirs()) {
                    throw new IOException("path create failure");
                }
            }
            File noMedia = new File(path, ".nomedia");
            if (!noMedia.exists()) {
                if (!noMedia.createNewFile()) {
                    throw new IOException(" \".nomedia\" create failure");
                }
            }
        } catch (IOException e) {
            throw new Exception(e);
        }
        return path;
    }


}
